

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.feature.feature_selection &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.feature.feature_selection</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.feature.feature_selection</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>

<span class="c1">#</span>
<span class="c1">#  Copyright 2019 The FATE Authors. All Rights Reserved.</span>
<span class="c1">#</span>
<span class="c1">#  Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1">#  you may not use this file except in compliance with the License.</span>
<span class="c1">#  You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1">#  Unless required by applicable law or agreed to in writing, software</span>
<span class="c1">#  distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1">#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1">#  See the License for the specific language governing permissions and</span>
<span class="c1">#  limitations under the License.</span>
<span class="c1">#</span>

<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">operator</span>
<span class="kn">import</span> <span class="nn">random</span>

<span class="kn">from</span> <span class="nn">google.protobuf</span> <span class="k">import</span> <span class="n">json_format</span>

<span class="kn">from</span> <span class="nn">arch.api.proto</span> <span class="k">import</span> <span class="n">feature_selection_meta_pb2</span>
<span class="kn">from</span> <span class="nn">arch.api.proto</span> <span class="k">import</span> <span class="n">feature_selection_param_pb2</span>
<span class="kn">from</span> <span class="nn">arch.api.utils</span> <span class="k">import</span> <span class="n">log_utils</span>
<span class="kn">from</span> <span class="nn">federatedml.param.param</span> <span class="k">import</span> <span class="n">UniqueValueParam</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic.data_overview</span> <span class="k">import</span> <span class="n">get_header</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic.statics</span> <span class="k">import</span> <span class="n">MultivariateStatisticalSummary</span>
<span class="kn">from</span> <span class="nn">federatedml.util</span> <span class="k">import</span> <span class="n">consts</span>

<span class="n">LOGGER</span> <span class="o">=</span> <span class="n">log_utils</span><span class="o">.</span><span class="n">getLogger</span><span class="p">()</span>


<div class="viewcode-block" id="FilterMethod"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.FilterMethod">[docs]</a><span class="k">class</span> <span class="nc">FilterMethod</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Use for filter columns</span>

<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    cols: list of int</span>
<span class="sd">        Col index to do selection</span>

<span class="sd">    left_cols: dict,</span>
<span class="sd">        k is col_index, value is bool that indicate whether it is left or not.</span>

<span class="sd">    feature_values: dict</span>
<span class="sd">        k is col_name, v is the value that used to judge whether left or not.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host_feature_values</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="FilterMethod.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.FilterMethod.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Filter data_instances for the specified columns</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instances : DTable,</span>
<span class="sd">            Input data</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A list of index of columns left.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span></div>

    <span class="c1"># def display_feature_result(self, party_name=&#39;Base&#39;):</span>
    <span class="c1">#     class_name = self.__class__.__name__</span>
    <span class="c1">#     for col_name, feature_value in self.feature_values.items():</span>
    <span class="c1">#         LOGGER.info(&quot;[Result][FeatureSelection][{}], in {}, col: {} &#39;s feature value is {}&quot;.format(</span>
    <span class="c1">#             party_name, class_name, col_name, feature_value</span>
    <span class="c1">#         ))</span>

    <span class="k">def</span> <span class="nf">_keep_one_feature</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pick_high</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">left_cols</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">feature_values</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Make sure at least one feature can be left after filtering.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        pick_high: bool</span>
<span class="sd">            Set when none of value left, choose the highest one or lowest one. True means highest one while</span>
<span class="sd">            False means lowest one.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        A list of index of columns left.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">left_cols</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span>

        <span class="k">if</span> <span class="n">feature_values</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">feature_values</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span>

        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;In _keep_one_feature, left_cols: </span><span class="si">{}</span><span class="s2">, feature_values: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">left_cols</span><span class="p">,</span> <span class="n">feature_values</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">is_left</span> <span class="ow">in</span> <span class="n">left_cols</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">is_left</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">left_cols</span>

        <span class="c1"># random pick one</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">feature_values</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">left_key</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">left_cols</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="c1"># pick the column with highest value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">left_cols</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">reverse</span><span class="o">=</span><span class="n">pick_high</span><span class="p">)</span>
            <span class="n">left_key</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>

        <span class="n">left_cols</span><span class="p">[</span><span class="n">left_key</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;In _keep_one_feature, left_key: </span><span class="si">{}</span><span class="s2">, left_cols: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">left_key</span><span class="p">,</span> <span class="n">left_cols</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">left_cols</span>

<div class="viewcode-block" id="FilterMethod.get_meta_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.FilterMethod.get_meta_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_meta_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="FilterMethod.get_param_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.FilterMethod.get_param_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_param_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span></div>

    <span class="k">def</span> <span class="nf">_init_cols</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="c1"># Already initialized</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="n">data_instances</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="n">header</span> <span class="o">=</span> <span class="n">get_header</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="n">header</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">header</span>

        <span class="k">for</span> <span class="n">col_index</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">:</span>
            <span class="n">col_name</span> <span class="o">=</span> <span class="n">header</span><span class="p">[</span><span class="n">col_index</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">col_index</span>

<div class="viewcode-block" id="FilterMethod.filter_one_party"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.FilterMethod.filter_one_party">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">filter_one_party</span><span class="p">(</span><span class="n">party_variances</span><span class="p">,</span> <span class="n">pick_high</span><span class="p">,</span> <span class="n">value_threshold</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">left_cols</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_info</span><span class="p">,</span> <span class="n">var_value</span> <span class="ow">in</span> <span class="n">party_variances</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">header</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">col_idx</span> <span class="o">=</span> <span class="n">header</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">col_info</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">col_idx</span> <span class="o">=</span> <span class="n">col_info</span>

            <span class="k">if</span> <span class="n">pick_high</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">var_value</span> <span class="o">&gt;</span> <span class="n">value_threshold</span><span class="p">:</span>
                    <span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">var_value</span> <span class="o">&lt;</span> <span class="n">value_threshold</span><span class="p">:</span>
                    <span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="n">left_cols</span></div>

    <span class="k">def</span> <span class="nf">_reset_data_instances</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="n">data_instances</span><span class="o">.</span><span class="n">schema</span><span class="p">[</span><span class="s1">&#39;header&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span>

    <span class="k">def</span> <span class="nf">_generate_col_name_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Used to adapt proto result.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">left_col_name_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_idx</span><span class="p">,</span> <span class="n">is_left</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;in _generate_col_name_dict, col_idx: </span><span class="si">{}</span><span class="s2">, type: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">col_idx</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">col_idx</span><span class="p">)))</span>
            <span class="n">col_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span>
            <span class="n">left_col_name_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">is_left</span>
        <span class="k">return</span> <span class="n">left_col_name_dict</span></div>


<div class="viewcode-block" id="UnionPercentileFilter"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.UnionPercentileFilter">[docs]</a><span class="k">class</span> <span class="nc">UnionPercentileFilter</span><span class="p">(</span><span class="n">FilterMethod</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Use for all union percentile filter methods</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    local_variance : list,</span>
<span class="sd">        The variance of guest party</span>

<span class="sd">    host_variances : list,</span>
<span class="sd">        The variance of guest party</span>

<span class="sd">    percentile : float</span>
<span class="sd">        The threshold percentile</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">local_variance</span><span class="p">,</span> <span class="n">host_variances</span><span class="p">,</span> <span class="n">percentile</span><span class="p">,</span> <span class="n">pick_high</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">UnionPercentileFilter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">local_variance</span> <span class="o">=</span> <span class="n">local_variance</span>  <span class="c1"># dict</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host_variances</span> <span class="o">=</span> <span class="n">host_variances</span>  <span class="c1"># dict of dict</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">percentiles</span> <span class="o">=</span> <span class="n">percentile</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pick_high</span> <span class="o">=</span> <span class="n">pick_high</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="n">header</span>

<div class="viewcode-block" id="UnionPercentileFilter.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.UnionPercentileFilter.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Fit local variances and each host vaiances</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instances : Useless, exist for extension</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Before get_value_threshold, host_variances: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">host_variances</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">get_value_threshold</span><span class="p">()</span>
        <span class="n">local_left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_one_party</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">local_variance</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">pick_high</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">)</span>
        <span class="n">local_left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">keep_one</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">local_variance</span><span class="p">,</span> <span class="n">local_left_cols</span><span class="p">)</span>
        <span class="n">host_left_cols</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_vaiances</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_variances</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">left_col</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_one_party</span><span class="p">(</span><span class="n">host_vaiances</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">pick_high</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span><span class="p">)</span>
            <span class="n">left_col</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">keep_one</span><span class="p">(</span><span class="n">host_vaiances</span><span class="p">,</span> <span class="n">left_col</span><span class="p">)</span>
            <span class="n">host_left_cols</span><span class="p">[</span><span class="n">host_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">left_col</span>
        <span class="k">return</span> <span class="n">local_left_cols</span><span class="p">,</span> <span class="n">host_left_cols</span></div>

<div class="viewcode-block" id="UnionPercentileFilter.keep_one"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.UnionPercentileFilter.keep_one">[docs]</a>    <span class="k">def</span> <span class="nf">keep_one</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variances</span><span class="p">,</span> <span class="n">left_cols</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">variances</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">left_cols</span>

        <span class="k">for</span> <span class="n">col_idx</span><span class="p">,</span> <span class="n">is_left</span> <span class="ow">in</span> <span class="n">left_cols</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">is_left</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">left_cols</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pick_high</span><span class="p">:</span>
            <span class="n">max_value</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;-inf&#39;</span><span class="p">)</span>
            <span class="n">max_key</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">feature_value</span> <span class="ow">in</span> <span class="n">variances</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">feature_value</span> <span class="o">&gt;</span> <span class="n">max_value</span><span class="p">:</span>
                    <span class="n">max_value</span> <span class="o">=</span> <span class="n">feature_value</span>
                    <span class="n">max_key</span> <span class="o">=</span> <span class="n">col_name</span>
            <span class="n">left_cols</span><span class="p">[</span><span class="n">max_key</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">min_value</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;inf&#39;</span><span class="p">)</span>
            <span class="n">min_key</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">feature_value</span> <span class="ow">in</span> <span class="n">variances</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">feature_value</span> <span class="o">&lt;</span> <span class="n">min_value</span><span class="p">:</span>
                    <span class="n">min_value</span> <span class="o">=</span> <span class="n">feature_value</span>
                    <span class="n">min_key</span> <span class="o">=</span> <span class="n">col_name</span>
            <span class="n">left_cols</span><span class="p">[</span><span class="n">min_key</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="n">left_cols</span></div>

<div class="viewcode-block" id="UnionPercentileFilter.get_value_threshold"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.UnionPercentileFilter.get_value_threshold">[docs]</a>    <span class="k">def</span> <span class="nf">get_value_threshold</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">total_values</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">total_values</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">local_variance</span><span class="o">.</span><span class="n">values</span><span class="p">()))</span>

        <span class="k">for</span> <span class="n">h_v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_variances</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">total_values</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">h_v</span><span class="o">.</span><span class="n">values</span><span class="p">()))</span>

        <span class="n">sorted_value</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">total_values</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">pick_high</span><span class="p">)</span>
        <span class="n">thres_idx</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">percentiles</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">sorted_value</span><span class="p">)</span> <span class="o">-</span> <span class="n">consts</span><span class="o">.</span><span class="n">FLOAT_ZERO</span><span class="p">))</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
            <span class="s2">&quot;sorted_value: </span><span class="si">{}</span><span class="s2">, thres_idx: </span><span class="si">{}</span><span class="s2">, len_sort_value: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sorted_value</span><span class="p">,</span> <span class="n">thres_idx</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">sorted_value</span><span class="p">)))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span> <span class="o">=</span> <span class="n">sorted_value</span><span class="p">[</span><span class="n">thres_idx</span><span class="p">]</span></div></div>


<div class="viewcode-block" id="UniqueValueFilter"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.UniqueValueFilter">[docs]</a><span class="k">class</span> <span class="nc">UniqueValueFilter</span><span class="p">(</span><span class="n">FilterMethod</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    filter the columns if all values in this feature is the same</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    param : UniqueValueParam object,</span>
<span class="sd">            Parameters that user set.</span>

<span class="sd">    cols : list of string or -1</span>
<span class="sd">            Specify which column(s) need to apply binning. -1 means do binning for all columns.</span>

<span class="sd">    statics_obj : MultivariateStatisticalSummary object, default: None</span>
<span class="sd">            If those static information has been compute. This can be use as parameter so that no need to</span>
<span class="sd">            compute again.</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param</span><span class="p">:</span> <span class="n">UniqueValueParam</span><span class="p">,</span>
                 <span class="n">cols</span><span class="p">,</span>
                 <span class="n">statics_obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">UniqueValueFilter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eps</span> <span class="o">=</span> <span class="n">param</span><span class="o">.</span><span class="n">eps</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">cols</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span> <span class="o">=</span> <span class="n">statics_obj</span>

<div class="viewcode-block" id="UniqueValueFilter.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.UniqueValueFilter.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span> <span class="o">=</span> <span class="n">MultivariateStatisticalSummary</span><span class="p">(</span><span class="n">data_instances</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>

        <span class="n">left_cols</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">max_values</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span><span class="o">.</span><span class="n">get_max</span><span class="p">()</span>
        <span class="n">min_values</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span><span class="o">.</span><span class="n">get_min</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">col_idx</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">:</span>
            <span class="n">col_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span>
            <span class="n">min_max_diff</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">max_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">-</span> <span class="n">min_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">min_max_diff</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">:</span>
                <span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">min_max_diff</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span> <span class="o">=</span> <span class="n">left_cols</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_keep_one_feature</span><span class="p">(</span><span class="n">pick_high</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reset_data_instances</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">left_cols</span></div>

<div class="viewcode-block" id="UniqueValueFilter.get_param_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.UniqueValueFilter.get_param_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_param_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">left_col_name_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate_col_name_dict</span><span class="p">()</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">]</span>
        <span class="n">left_col_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">LeftCols</span><span class="p">(</span><span class="n">original_cols</span><span class="o">=</span><span class="n">cols</span><span class="p">,</span>
                                                            <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_name_dict</span><span class="p">)</span>

        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">FeatureSelectionFilterParam</span><span class="p">(</span><span class="n">feature_values</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">,</span>
                                                                         <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_obj</span><span class="p">,</span>
                                                                         <span class="n">filter_name</span><span class="o">=</span><span class="s2">&quot;UNIQUE FILTER&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="UniqueValueFilter.get_meta_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.UniqueValueFilter.get_meta_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_meta_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_meta_pb2</span><span class="o">.</span><span class="n">UniqueValueMeta</span><span class="p">(</span><span class="n">eps</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div></div>


<div class="viewcode-block" id="IVValueSelectFilter"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.IVValueSelectFilter">[docs]</a><span class="k">class</span> <span class="nc">IVValueSelectFilter</span><span class="p">(</span><span class="n">FilterMethod</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Drop the columns if their iv is smaller than a threshold</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    param : IVSelectionParam object,</span>
<span class="sd">            Parameters that user set.</span>

<span class="sd">    cols : list of int</span>
<span class="sd">            Specify header of guest variances.</span>

<span class="sd">    binning_obj : Binning object</span>
<span class="sd">        Use for collecting iv among all parties.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">binning_obj</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">IVValueSelectFilter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span> <span class="o">=</span> <span class="n">param</span><span class="o">.</span><span class="n">value_threshold</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">cols</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">binning_obj</span> <span class="o">=</span> <span class="n">binning_obj</span>

<div class="viewcode-block" id="IVValueSelectFilter.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.IVValueSelectFilter.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># fit guest</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="n">guest_binning_result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">binning_obj</span><span class="o">.</span><span class="n">binning_result</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">iv_attr</span> <span class="ow">in</span> <span class="n">guest_binning_result</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">col_idx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">col_idx</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">iv_attr</span><span class="o">.</span><span class="n">iv</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_one_party</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_keep_one_feature</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_bin_result</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">binning_obj</span><span class="o">.</span><span class="n">host_results</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">tmp_host_value</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">host_col_name</span><span class="p">,</span> <span class="n">host_iv_attr</span> <span class="ow">in</span> <span class="n">host_bin_result</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">tmp_host_value</span><span class="p">[</span><span class="n">host_col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_iv_attr</span><span class="o">.</span><span class="n">iv</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">host_feature_values</span><span class="p">[</span><span class="n">host_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp_host_value</span>
            <span class="n">left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter_one_party</span><span class="p">(</span><span class="n">tmp_host_value</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span><span class="p">)</span>
            <span class="n">left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_keep_one_feature</span><span class="p">(</span><span class="n">left_cols</span><span class="o">=</span><span class="n">left_cols</span><span class="p">,</span> <span class="n">feature_values</span><span class="o">=</span><span class="n">tmp_host_value</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span><span class="p">[</span><span class="n">host_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">left_cols</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span></div>

<div class="viewcode-block" id="IVValueSelectFilter.get_param_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.IVValueSelectFilter.get_param_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_param_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">left_col_name_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate_col_name_dict</span><span class="p">()</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">]</span>

        <span class="n">host_obj</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;In get_param_obj, host_cols: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_left_cols</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">host_cols</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">host_left_cols</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>

            <span class="c1"># new_host_left_cols = {}</span>
            <span class="c1"># for k, v in host_left_cols.items():</span>
            <span class="c1">#     new_host_left_cols[str(k)] = v</span>

            <span class="n">host_left_col_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">LeftCols</span><span class="p">(</span><span class="n">original_cols</span><span class="o">=</span><span class="n">host_cols</span><span class="p">,</span>
                                                                     <span class="n">left_cols</span><span class="o">=</span><span class="n">host_left_cols</span><span class="p">)</span>
            <span class="n">host_obj</span><span class="p">[</span><span class="n">host_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_left_col_obj</span>

            <span class="k">for</span> <span class="n">host_col</span><span class="p">,</span> <span class="n">is_left</span> <span class="ow">in</span> <span class="n">host_left_cols</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">new_col_name</span> <span class="o">=</span> <span class="s1">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">host_name</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">host_col</span><span class="p">)])</span>
                <span class="n">cols</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_col_name</span><span class="p">)</span>
                <span class="n">left_col_name_dict</span><span class="p">[</span><span class="n">new_col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">is_left</span>

        <span class="n">left_col_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">LeftCols</span><span class="p">(</span><span class="n">original_cols</span><span class="o">=</span><span class="n">cols</span><span class="p">,</span>
                                                            <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_name_dict</span><span class="p">)</span>

        <span class="n">host_value_objs</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_feature_values</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_feature_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">host_feature_value_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">FeatureValue</span><span class="p">(</span><span class="n">feature_values</span><span class="o">=</span><span class="n">host_feature_values</span><span class="p">)</span>
            <span class="n">host_value_objs</span><span class="p">[</span><span class="n">host_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_feature_value_obj</span>

        <span class="c1"># Combine both guest and host results</span>
        <span class="n">total_feature_values</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">col_value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">total_feature_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">col_value</span>

        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_feature_values</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_feature_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">host_col</span><span class="p">,</span> <span class="n">host_feature_value</span> <span class="ow">in</span> <span class="n">host_feature_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">new_col_name</span> <span class="o">=</span> <span class="s1">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">host_name</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">host_col</span><span class="p">)])</span>
                <span class="n">total_feature_values</span><span class="p">[</span><span class="n">new_col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_feature_value</span>

        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">FeatureSelectionFilterParam</span><span class="p">(</span><span class="n">feature_values</span><span class="o">=</span><span class="n">total_feature_values</span><span class="p">,</span>
                                                                         <span class="n">host_feature_values</span><span class="o">=</span><span class="n">host_value_objs</span><span class="p">,</span>
                                                                         <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_obj</span><span class="p">,</span>
                                                                         <span class="n">host_left_cols</span><span class="o">=</span><span class="n">host_obj</span><span class="p">,</span>
                                                                         <span class="n">filter_name</span><span class="o">=</span><span class="s2">&quot;IV_VALUE_FILTER&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="IVValueSelectFilter.get_meta_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.IVValueSelectFilter.get_meta_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_meta_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_meta_pb2</span><span class="o">.</span><span class="n">IVValueSelectionMeta</span><span class="p">(</span><span class="n">value_threshold</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div></div>


<div class="viewcode-block" id="IVPercentileFilter"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.IVPercentileFilter">[docs]</a><span class="k">class</span> <span class="nc">IVPercentileFilter</span><span class="p">(</span><span class="n">FilterMethod</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Drop the columns if their iv is smaller than a threshold of percentile.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    iv_param : IVSelectionParam object,</span>
<span class="sd">            Parameters that user set.</span>

<span class="sd">    cols : list of string</span>
<span class="sd">            Specify header of guest variances.</span>

<span class="sd">    binning_obj : Binning object</span>
<span class="sd">        Use for collecting iv among all parties.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iv_param</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">host_cols</span><span class="p">,</span> <span class="n">binning_obj</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">IVPercentileFilter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iv_param</span> <span class="o">=</span> <span class="n">iv_param</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">percentile_thres</span> <span class="o">=</span> <span class="n">iv_param</span><span class="o">.</span><span class="n">percentile_threshold</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">cols</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span> <span class="o">=</span> <span class="n">host_cols</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">binning_obj</span> <span class="o">=</span> <span class="n">binning_obj</span>

<div class="viewcode-block" id="IVPercentileFilter.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.IVPercentileFilter.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="c1"># fit guest</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>

        <span class="n">guest_binning_result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">binning_obj</span><span class="o">.</span><span class="n">binning_result</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;In iv percentile, guest_binning_result: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">guest_binning_result</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">iv_attr</span> <span class="ow">in</span> <span class="n">guest_binning_result</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">col_idx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">col_idx</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">iv_attr</span><span class="o">.</span><span class="n">iv</span>

        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;self.feature_values: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">))</span>

        <span class="n">host_feature_values</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_bin_result</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">binning_obj</span><span class="o">.</span><span class="n">host_results</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">host_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">host_to_select_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">host_name</span><span class="p">)</span>
            <span class="n">tmp_host_value</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">host_col_idx</span><span class="p">,</span> <span class="n">host_iv_attr</span> <span class="ow">in</span> <span class="n">host_bin_result</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">host_col_idx</span><span class="p">)</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">host_to_select_cols</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="n">tmp_host_value</span><span class="p">[</span><span class="n">host_col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_iv_attr</span><span class="o">.</span><span class="n">iv</span>
            <span class="n">host_feature_values</span><span class="p">[</span><span class="n">host_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp_host_value</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">host_feature_values</span> <span class="o">=</span> <span class="n">host_feature_values</span>
        <span class="n">union_filter</span> <span class="o">=</span> <span class="n">UnionPercentileFilter</span><span class="p">(</span><span class="n">local_variance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">,</span>
                                             <span class="n">host_variances</span><span class="o">=</span><span class="n">host_feature_values</span><span class="p">,</span>
                                             <span class="n">percentile</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">percentile_thres</span><span class="p">,</span>
                                             <span class="n">pick_high</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                             <span class="n">header</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">)</span>
        <span class="n">local_left_cols</span><span class="p">,</span> <span class="n">host_left_cols</span> <span class="o">=</span> <span class="n">union_filter</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span> <span class="o">=</span> <span class="n">local_left_cols</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span> <span class="o">=</span> <span class="n">host_left_cols</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span></div>

<div class="viewcode-block" id="IVPercentileFilter.get_param_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.IVPercentileFilter.get_param_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_param_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">left_col_name_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate_col_name_dict</span><span class="p">()</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">]</span>

        <span class="n">host_obj</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_left_cols</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_cols</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">host_cols</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">host_left_cols</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
            <span class="n">host_left_col_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">LeftCols</span><span class="p">(</span><span class="n">original_cols</span><span class="o">=</span><span class="n">host_cols</span><span class="p">,</span>
                                                                     <span class="n">left_cols</span><span class="o">=</span><span class="n">host_left_cols</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">host_col</span><span class="p">,</span> <span class="n">is_left</span> <span class="ow">in</span> <span class="n">host_left_cols</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">new_col_name</span> <span class="o">=</span> <span class="s1">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">host_name</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">host_col</span><span class="p">)])</span>
                <span class="n">cols</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_col_name</span><span class="p">)</span>
                <span class="n">left_col_name_dict</span><span class="p">[</span><span class="n">new_col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">is_left</span>

            <span class="n">host_obj</span><span class="p">[</span><span class="n">host_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_left_col_obj</span>

        <span class="n">left_col_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">LeftCols</span><span class="p">(</span><span class="n">original_cols</span><span class="o">=</span><span class="n">cols</span><span class="p">,</span>
                                                            <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_name_dict</span><span class="p">)</span>

        <span class="n">host_value_objs</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_feature_values</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_feature_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">host_feature_value_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">FeatureValue</span><span class="p">(</span><span class="n">feature_values</span><span class="o">=</span><span class="n">host_feature_values</span><span class="p">)</span>
            <span class="n">host_value_objs</span><span class="p">[</span><span class="n">host_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_feature_value_obj</span>

        <span class="c1"># Combine both guest and host results</span>
        <span class="n">total_feature_values</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">col_value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">total_feature_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">col_value</span>

        <span class="k">for</span> <span class="n">host_name</span><span class="p">,</span> <span class="n">host_feature_values</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">host_feature_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">host_col</span><span class="p">,</span> <span class="n">host_feature_value</span> <span class="ow">in</span> <span class="n">host_feature_values</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">new_col_name</span> <span class="o">=</span> <span class="s1">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">host_name</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">host_col</span><span class="p">)])</span>
                <span class="n">total_feature_values</span><span class="p">[</span><span class="n">new_col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">host_feature_value</span>

        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">FeatureSelectionFilterParam</span><span class="p">(</span><span class="n">feature_values</span><span class="o">=</span><span class="n">total_feature_values</span><span class="p">,</span>
                                                                         <span class="n">host_feature_values</span><span class="o">=</span><span class="n">host_value_objs</span><span class="p">,</span>
                                                                         <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_obj</span><span class="p">,</span>
                                                                         <span class="n">host_left_cols</span><span class="o">=</span><span class="n">host_obj</span><span class="p">,</span>
                                                                         <span class="n">filter_name</span><span class="o">=</span><span class="s1">&#39;IV_PERCENTILE&#39;</span><span class="p">)</span>
        <span class="n">json_result</span> <span class="o">=</span> <span class="n">json_format</span><span class="o">.</span><span class="n">MessageToJson</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="n">including_default_value_fields</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;json_result: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">json_result</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="IVPercentileFilter.get_meta_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.IVPercentileFilter.get_meta_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_meta_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_meta_pb2</span><span class="o">.</span><span class="n">IVPercentileSelectionMeta</span><span class="p">(</span><span class="n">percentile_threshold</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">percentile_thres</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div></div>


<div class="viewcode-block" id="CoeffOfVarValueFilter"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.CoeffOfVarValueFilter">[docs]</a><span class="k">class</span> <span class="nc">CoeffOfVarValueFilter</span><span class="p">(</span><span class="n">FilterMethod</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Drop the columns if their coefficient of varaiance is smaller than a threshold.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    param : CoeffOfVarSelectionParam object,</span>
<span class="sd">            Parameters that user set.</span>

<span class="sd">    cols : list of string</span>
<span class="sd">            Specify header of guest variances.</span>

<span class="sd">    statics_obj : MultivariateStatisticalSummary object, default: None</span>
<span class="sd">            If those static information has been compute. This can be use as parameter so that no need to</span>
<span class="sd">            compute again.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">param</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">statics_obj</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">CoeffOfVarValueFilter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span> <span class="o">=</span> <span class="n">param</span><span class="o">.</span><span class="n">value_threshold</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">cols</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span> <span class="o">=</span> <span class="n">statics_obj</span>

<div class="viewcode-block" id="CoeffOfVarValueFilter.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.CoeffOfVarValueFilter.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span> <span class="o">=</span> <span class="n">MultivariateStatisticalSummary</span><span class="p">(</span><span class="n">data_instances</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>

        <span class="n">std_var</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span><span class="o">.</span><span class="n">get_std_variance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">)</span>
        <span class="n">mean_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">statics_obj</span><span class="o">.</span><span class="n">get_mean</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">s_v</span> <span class="ow">in</span> <span class="n">std_var</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">col_idx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span>
            <span class="n">mean</span> <span class="o">=</span> <span class="n">mean_value</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span>
            <span class="n">coeff_of_var</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">s_v</span> <span class="o">/</span> <span class="n">mean</span><span class="p">)</span>
            <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;In var_coe, col_name: </span><span class="si">{}</span><span class="s2">, col_idx: </span><span class="si">{}</span><span class="s2">, mean: </span><span class="si">{}</span><span class="s2">, std: </span><span class="si">{}</span><span class="s2">, coeff_of_var: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">col_name</span><span class="p">,</span> <span class="n">col_idx</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">s_v</span><span class="p">,</span> <span class="n">coeff_of_var</span>
            <span class="p">))</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">coeff_of_var</span>
            <span class="k">if</span> <span class="n">coeff_of_var</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_keep_one_feature</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reset_data_instances</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span></div>

<div class="viewcode-block" id="CoeffOfVarValueFilter.get_param_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.CoeffOfVarValueFilter.get_param_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_param_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">left_col_name_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate_col_name_dict</span><span class="p">()</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">]</span>

        <span class="n">left_col_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">LeftCols</span><span class="p">(</span><span class="n">original_cols</span><span class="o">=</span><span class="n">cols</span><span class="p">,</span>
                                                            <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_name_dict</span><span class="p">)</span>

        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">FeatureSelectionFilterParam</span><span class="p">(</span>
            <span class="n">feature_values</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">,</span>
            <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_obj</span><span class="p">,</span>
            <span class="n">filter_name</span><span class="o">=</span><span class="s2">&quot;COEFFICIENT OF VARIANCE&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="CoeffOfVarValueFilter.get_meta_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.CoeffOfVarValueFilter.get_meta_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_meta_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_meta_pb2</span><span class="o">.</span><span class="n">VarianceOfCoeSelectionMeta</span><span class="p">(</span><span class="n">value_threshold</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">value_threshold</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div></div>


<div class="viewcode-block" id="OutlierFilter"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.OutlierFilter">[docs]</a><span class="k">class</span> <span class="nc">OutlierFilter</span><span class="p">(</span><span class="n">FilterMethod</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given percentile and threshold. Judge if this quantile point is larger than threshold. Filter those larger ones.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    params : OutlierColsSelectionParam object,</span>
<span class="sd">            Parameters that user set.</span>

<span class="sd">    cols : list of int</span>
<span class="sd">            Specify which column(s) need to apply this filter method. -1 means do binning for all columns.</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">cols</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">OutlierFilter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">percentile</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">percentile</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">upper_threshold</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">upper_threshold</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">cols</span>

<div class="viewcode-block" id="OutlierFilter.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.OutlierFilter.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">,</span> <span class="n">bin_param</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="c1"># bin_obj = QuantileBinning(bin_param)</span>
        <span class="n">summary_obj</span> <span class="o">=</span> <span class="n">MultivariateStatisticalSummary</span><span class="p">(</span><span class="n">data_instances</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>
        <span class="n">query_result</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_quantile_point</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">percentile</span><span class="p">)</span>
        <span class="c1"># query_result = bin_obj.query_quantile_point(data_instances, self.cols, self.percentile)</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">feature_value</span> <span class="ow">in</span> <span class="n">query_result</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">col_idx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">feature_value</span>
            <span class="k">if</span> <span class="n">feature_value</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">upper_threshold</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span><span class="p">[</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_keep_one_feature</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reset_data_instances</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">left_cols</span></div>

<div class="viewcode-block" id="OutlierFilter.get_param_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.OutlierFilter.get_param_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_param_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">left_col_name_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate_col_name_dict</span><span class="p">()</span>
        <span class="n">cols</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">]</span>

        <span class="n">left_col_obj</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">LeftCols</span><span class="p">(</span><span class="n">original_cols</span><span class="o">=</span><span class="n">cols</span><span class="p">,</span>
                                                            <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_name_dict</span><span class="p">)</span>

        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_param_pb2</span><span class="o">.</span><span class="n">FeatureSelectionFilterParam</span><span class="p">(</span><span class="n">feature_values</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_values</span><span class="p">,</span>
                                                                         <span class="n">left_cols</span><span class="o">=</span><span class="n">left_col_obj</span><span class="p">,</span>
                                                                         <span class="n">filter_name</span><span class="o">=</span><span class="s2">&quot;OUTLIER&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="OutlierFilter.get_meta_obj"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.feature_selection.OutlierFilter.get_meta_obj">[docs]</a>    <span class="k">def</span> <span class="nf">get_meta_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">feature_selection_meta_pb2</span><span class="o">.</span><span class="n">OutlierColsSelectionMeta</span><span class="p">(</span><span class="n">percentile</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">percentile</span><span class="p">,</span>
                                                                     <span class="n">upper_threshold</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">upper_threshold</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>